package com.wodogs.utils.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.Cookie;

import com.google.gson.Gson;
import com.wodogs.utils.bean.WfAccount;
import com.wodogs.utils.tools.HttpMry;
import com.wodogs.utils.tools.ToolHttp;

public class AccountService {
	
	private static String RESET_LIST_URL = "http://127.0.0.1:89/mercury/account/resetList/";
	
	private static String UDPATE_COOKIE_URL = "http://127.0.0.1:89/mercury/account/udpateCookie";
	
//	private static String LOGIN_SUCCESS_URL = "http://www.bilibili.com/";

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List<WfAccount> getResetList(String web_site,Integer status){
		List<WfAccount> list = new ArrayList<WfAccount>();
		if(StringUtils.isBlank(web_site)){
			return list;
		}
		String url = RESET_LIST_URL+web_site;
		if(status!=null){
			url +=("?status="+status);
		}
		String accountStr = ToolHttp.get(false,url );
    	try{
    		
    		Gson gson = new Gson();
    		List listCount = gson.fromJson(accountStr, List.class);
    		if(listCount==null||listCount.size()<1){
    			return list;
    		}
    		for(int i=0;i<listCount.size();i++){
    			Map<String,String> map = (Map<String,String>)listCount.get(i);
    			String id = map.get("id");
    			String login_name = map.get("login_name");
    			String password = map.get("password");
    			String last_proxy = map.get("last_proxy");
    			String cookies = map.get("cookies");
    			String cookies_full = map.get("cookies_full");
    			list.add(new WfAccount(id, login_name, password,last_proxy,cookies,cookies_full));
    		}
    	}catch(Exception ex){
    		System.err.println(ex.getMessage());
    		return list;
    	}
    	return list;
	}
	
	public static String getLashProxy(List<WfAccount> list){
		if(list == null){
			return null;
		}
		StringBuilder sb = new StringBuilder("");
		for(WfAccount a:list){
			if(StringUtils.isNotBlank(a.getLast_proxy())){
				sb.append(a.getLast_proxy()+",");
			}
		}
		String str = sb.toString();
		if(str.endsWith(",")){
			return str.substring(0, str.length()-1);
		}else{
			return str;
		}
	}
	
	public static boolean udpateCookieAndProxy(String id,String cookies, String proxyId){
		if(StringUtils.isBlank(id)||StringUtils.isBlank(cookies)){
			return false;
		}
		Map<String,String> map = new HashMap<String,String>();
		map.put("id", id);
		map.put("updateCookies", cookies);
		map.put("proxyId", proxyId);
		return HttpMry.post(UDPATE_COOKIE_URL, map);
//		Gson gson = new Gson();
//		String contentType= "application/x-www-form-urlencoded; charset=UTF-8";
//		String resultStr = ToolHttp.post(false, UDPATE_COOKIE_URL, gson.toJson(map), contentType);
//    	try{
//    		AppResult result = gson.fromJson(resultStr, AppResult.class);
//    		if(result==null||result.getStatus()!=1){
//    			return false;
//    		}
//    		return true;
//    	}catch(Exception ex){
//    		System.err.println(ex.getMessage());
//    		return false;
//    	}
	}
	
	public static boolean udpateCookieAndProxy(String id,Set<Cookie> cookies, String proxyId){
		if(StringUtils.isBlank(id)||cookies == null || cookies.isEmpty()){
			return false;
		}
		
		StringBuilder cookieSB = new StringBuilder("");
		for(Cookie c:cookies){
			cookieSB.append(c.getName()+"="+c.getValue()+";");
        }
		String cookieStr = cookieSB.toString();
		Gson gson = new Gson();
		String cookiesJson = gson.toJson(cookies);
       
        Map<String,String> map = new HashMap<String,String>();
		map.put("id", id);
		map.put("updateCookies", cookieStr);
		map.put("cookies_full", cookiesJson);
		map.put("proxyId", proxyId);
		return HttpMry.post(UDPATE_COOKIE_URL, map);
	}
}
